﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LongestSequence.Question;

namespace LongestSequence.Answer
{
    public class Bob : ILongestSequence
    {
        int[] numbers; 
        public int GetLongestSequence(int[] Array)
        {
            numbers = Array;
            int res = 0;
            for (int i = 0; i < Array.Length; i++)
            {
                for (int j = i; j < Array.Length; j++)
                {
                    if (VerfiySequence(i, j))
                    {
                        int cur = j - i + 1;
                       res = Math.Max(cur, res);

                    }
                }
            }
            return res;
        }


        private bool VerfiySequence(int start, int end)
        {
            int length = end - start + 1;
            int[] cur = new int[length];
            
            Array.ConstrainedCopy(numbers, start, cur, 0, length);
            cur = Array.ConvertAll(cur, new Converter<int, int>(i => i == 0 ? int.MinValue : i)); ;
                

            Array.Sort(cur);
            int zeroCnt = 0;
            bool first = true;
            int distence = 0;
            for (int i = 0; i < cur.Length; i++)
            {
                if (cur[i] == int.MinValue)
                {
                    zeroCnt++;
                }
                else if (first)
                {
                    first = false;
                }
                else
                {
                    distence += cur[i] - cur[i - 1] - 1;
                }
            }
            if (distence > zeroCnt)
            {
                return false;
            }
            return true;
            
        }

    }
}
